extends StateBase


var player: Player


func on_enter(owner = null, params = null):
	player = owner as Player
	
	var knock_back_velocity = player.current_hit_data.knockback - player.player_data.get_anti_knockback()
	if knock_back_velocity <= 0:
		knock_back_velocity = 0
	
	var is_critical = player.current_hit_data.is_critical
	if is_critical:
		player.level.emit_particle("particle_critical_attack_effect", player.get_random_hurt_position())
		player.light_animation_player.play("CriticalLightFlash")
		
	player.velocity.x = player.get_face() * -1 * knock_back_velocity
	player.velocity.y = player.current_hit_data.affect_vertical_velocity
	player.level.emit_particle("particle_hit_blood", player.get_random_hurt_position())
	
	var damage_scale = 1.5 if is_critical else 1.0
	var damage = player.current_hit_data.actual_damage
	player.level.raise_damage_number(damage, player.get_random_hurt_position() + Vector2.UP * 16, damage_scale)
	
	if knock_back_velocity:
		await get_tree().create_timer(0.1).timeout
		
	if player.is_dead:
		player.states.transition("Death")
	else:
		player.states.transition("Move")
	

func on_exit(owner = null):
	pass


func on_process(delta: float, owner = null):
	pass
	
	
func on_physics_process(delta: float, owner = null):
	player.process_gravity(delta)
	player.process_vertical_move_viewroot(delta)
	player.process_pickup()
	
	player.move_and_slide()
	
	player.process_gun_hand_rotation()
